package stark.app.frame.qrcode;

/**
 * 更多功能：https://github.com/jenly1314/ZXingLite
 */

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ToastUtils;
import com.king.zxing.util.CodeUtils;

import stark.app.R;
import stark.common.basic.constant.Extra;
import stark.common.basic.utils.PermissionUtil;

public class QRCodeActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int REQ_QR = 1;
    ImageView mIvQRCode;
    EditText etContent;
    TextView tvScanRet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_q_r_code);

        findViewById(R.id.btnScan).setOnClickListener(this);
        findViewById(R.id.btnGenerate).setOnClickListener(this);
        mIvQRCode = findViewById(R.id.ivQRCode);
        etContent = findViewById(R.id.etContent);
        tvScanRet = findViewById(R.id.tvScanRet);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btnScan) {
            PermissionUtil.with(this).permission(Manifest.permission.CAMERA)
                    .request(granted -> {
                        if (granted) { // Always true pre-M
                            QRCodeScanActivity.startForRet(QRCodeActivity.this, REQ_QR);
                        } else {
                            Toast.makeText(this, "请允许相机权限", Toast.LENGTH_SHORT).show();
                        }
                    });
        } else if (id == R.id.btnGenerate) {
            createQRCode();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQ_QR) {
            String content = data.getStringExtra(Extra.CONTENT);
            tvScanRet.setText(content);
            tryGoBrowse(content);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 生成二维码
     */
    private void createQRCode() {
        String content = etContent.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort("请先输入要生成的内容");
            return;
        }
        new Thread(() -> {
            //生成二维码相关放在子线程里面
            Bitmap bitmap = CodeUtils.createQRCode(content, 600);
            runOnUiThread(() -> {
                //显示二维码
                mIvQRCode.setImageBitmap(bitmap);
            });
        }).start();

    }

    private void tryGoBrowse(String content) {
        if (TextUtils.isEmpty(content) || !content.startsWith("http")) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(content));
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        startActivity(intent);
    }
}